<template>
  <view class="drawback-wrap">
    <!-- #ifdef H5 -->
    <van-nav-bar @click-left="onClickLeft" left-arrow title="注销账户"></van-nav-bar>
    <!-- #endif -->
    <view class="coupon-content">
      <text class="title text-bold">账号：{{ data.accountNo }}</text>
      <view class="line"></view>
      <view class="title text-bold">
        <icon class="iconfont icon-tixing"></icon>
        <text class="text">账号注销须知</text>
      </view>
      <view class="info">
        <view>
          <view class="test1">账号申请注销前请先确认以下事宜。</view>
          <view class="test1">
            1、您是个人用户，即在移动端自行注册的账号，不是企业分发给您的账号（企业账号注销需联系企业管理员进行操作）。
          </view>
          <view class="test1">
            2、当前账号所有订单均完成结算，无充电中订单、异常订单、待支付订单，否则申请注销失败。
          </view>
          <view class="test1">
            3、账号注销后，您重新登录移动端将会注册新账号，原有账号已获得福利（例如：优惠券、会员权益等）不再享有。
          </view>
        </view>
      </view>
    </view>
    <view class="bottom-wrap" :style="styleBntH">
      <view class="agreement-wrap">
        <view class="radio-wrap" :class="{ 'radio-radius': !checkAgreement }" @tap="tapAgreement">
          <icon v-if="checkAgreement" :type="'success'" :color="colors.systemColor" size="20" />
        </view>
        <text>我已阅读并同意</text>
        <text class="text1" @tap="tapAgreementUrl">《注销协议》</text>
      </view>
      <button class="bnt-wrap bnt" @tap="applyDrawback">确认注销</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { getSystemInfo, setToastInstance, systemInfo } from '@mixins/info';
import { passportCancel } from '@/service/apis/mine';
import { colors, clearToken, clearUserId } from '@mixins/info';
import { getItem } from '@/utils/util';
// #ifdef MP-WEIXIN
import { useToast } from '@/components/wxToast';
setToastInstance(useToast());
// #endif

// 分享
onShareAppMessage(() => {
  return {
    title: '',
    path: '/pages/stationMap/stationMap',
  };
});

const $toast: any = inject('$Toast');

const styleBntH = `height: calc(186rpx + ${getSystemInfo().safeBottom}px)`;

const data = reactive({
  accountNo: '',
});

onLoad((option: any) => {
  console.log('option=====', option);
  data.accountNo = getItem('accountNumber');
});
let checkAgreement = ref(false); //是否勾选协议

// 协议
const tapAgreement = () => {
  checkAgreement.value = !checkAgreement.value;
};

const tapAgreementUrl = () => {
  const url = `${import.meta.env.VITE_VUE_APP_RES}${
    import.meta.env.VITE_VUE_APP_AGREEMENT
  }agreement-logout.html`;
  uni.navigateTo({
    url: `/pagesMain/webView/webView?url=${url}`,
  });
};

const applyDrawback = () => {
  if (!checkAgreement.value) {
    $toast('请勾选协议');
    return;
  }
  passportCancel({}, { account: { accountId: getItem('userid') } })
    .then((res: any) => {
      //   uni.redirectTo({
      //     url: '/pages/login/login',
      //   });
      uni.showModal({
        content: '您的账号已成功注销',
        showCancel: false,
        success: () => {
          clearToken();
          clearUserId();
          uni.reLaunch({
            url: '/pages/login/login',
          });
        },
      });
    })
    .catch((res: any) => {
      if (res.businessCode === '1') {
        uni.showModal({
          content: '您的账号已成功注销',
          showCancel: false,
          success: () => {
            clearToken();
            clearUserId();
            uni.reLaunch({
              url: '/pages/login/login',
            });
          },
        });
      }
      if (res.businessCode === '10221') {
        uni.showModal({
          content: res.msg,
          showCancel: false,
          success: () => {},
        });
      } else if (res.businessCode === '10222') {
        uni.showModal({
          content: res.msg,
          showCancel: true,
          confirmText: '去申请',
          success: e => {
            if (e.confirm) {
              uni.reLaunch({
                url: '/pagesMyCenter/drawback/drawback',
              });
            } else {
              console.log('cancel'); //点击取消之后执行的代码
            }
          },
        });
      } else if (res.businessCode === '10220') {
        uni.showModal({
          content: res.msg,
          showCancel: false,
          success: () => {},
        });
      } else if (res.businessCode === '10223') {
        uni.showModal({
          content: res.msg,
          showCancel: false,
          success: () => {},
        });
      } else {
        uni.showModal({
          content: res.msg,
          showCancel: false,
          success: () => {},
        });
      }
    });
  // console.log(11111);
};
// #ifdef H5
const onClickLeft = () => {
  uni.navigateBack();
};
setTimeout(() => {
  const navBarEle = document.querySelector('.van-nav-bar') as HTMLElement;
  navBarEle.style.paddingTop = `${systemInfo.statusBarHeight}px`;
});
// #endif
</script>

<style lang="scss" scoped>
.drawback-wrap {
  position: relative;
  height: calc(100vh - 12px);
  padding-top: 12px;
  background-color: #f6f8ff;
  overflow: hidden;
  // #ifdef H5
  height: 100vh;
  padding-top: 0;
  // #endif

  .coupon-content {
    margin: 12px 16px 12px 16px;
    padding: 16px;
    // margin-top: 12px;
    background-color: #fff;
    font-size: 14px;
    color: #545454;
    border-radius: 8px;

    &:last-child {
      border: 0;
    }

    .title {
      line-height: 24px;
      color: #000;
    }

    .info {
      display: flex;
      font-size: 14px;
      justify-content: space-between;

      .left {
        width: 30%;
        color: #858585;
      }

      .right {
        width: 70%;
        text-align: right;
        color: #242424;
      }

      .test {
        line-height: 22px;
        margin-top: 8px;

        .content-input {
          height: 25px;
        }
      }

      .test1 {
        line-height: 22px;
        margin-top: 8px;
        color: #545454;
      }

      .text-red {
        color: #fa3939;
      }
    }

    .line {
      margin: 12px 0;
      height: 1px;
      background-color: #f2f2f2;
      // border-bottom: 1px solid #f2f2f2;
    }
  }

  .bottom-wrap {
    // display: flex;
    display: inline-block;
    position: fixed;
    width: 100%;
    bottom: 0;
    background: #fff;
    border-top: 1px solid #f2f2f2;
    box-sizing: border-box;

    .bnt {
      height: 44px;
      width: 343px;
      margin-top: 9px;
      border-radius: 50px;
    }

    .agreement-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
      padding-top: 10px;

      .radio-wrap {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 20px;
        height: 20px;
        margin-right: 8px;
      }

      .radio-radius {
        border: 1px solid #bdbdbd;
        border-radius: 50%;
        box-sizing: border-box;
      }

      text {
        line-height: 22px;
        font-size: 14px;
      }

      .text1 {
        color: $uni-color-primary;
      }
    }
  }
}

.title {
  .icon-tixing {
    font-size: 16px;
    color: #fa3939;
  }

  .text {
    font-size: 16px;
    color: #fa3939;
  }
}
</style>